package com.allwees.bs.module.fms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.allwees.bs.core.modelbase.exception.BusinessException;
import com.allwees.bs.core.modelbase.req.Queryable;
import com.allwees.bs.module.base.service.impl.BoServiceImpl;
import com.allwees.bs.module.fms.dao.PlatformWithdrawAccountDao;
import com.allwees.bs.module.fms.entity.PlatformWithdrawAccount;
import com.allwees.bs.module.fms.req.PlatformWithdrawAccountInfoReq;
import com.allwees.bs.module.fms.service.PlatformWithdrawAccountService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 *
 * @author yangylsky
 * @since 2019-12-12
 *
 */
@Service
public class PlatformWithdrawAccountServiceImpl extends BoServiceImpl<PlatformWithdrawAccountDao, PlatformWithdrawAccount>
		implements PlatformWithdrawAccountService {
	@Override
	@Transactional(rollbackFor = Exception.class)
	public PlatformWithdrawAccount createAccount(PlatformWithdrawAccountInfoReq req) {
		if(findDefaultWithdrawAccount(req.getCurrentPlatformUuid()) != null) {
			throw new BusinessException("默认提现账户已存在");
		}
		PlatformWithdrawAccount withdrawAccount = req.toEntity();
		baseMapper.create(withdrawAccount);
		return withdrawAccount;
	}

	@Override
	public List<PlatformWithdrawAccount> findAllWithdrawAccount(String branchUuid) {
		return findList(new Queryable<PlatformWithdrawAccount>() {
			@Override
			public QueryWrapper<PlatformWithdrawAccount> toQueryWrapper() {
				return new QueryWrapper<PlatformWithdrawAccount>()
						.eq("branch_uuid", branchUuid);
			}
		});
	}

	@Override
	public PlatformWithdrawAccount findDefaultWithdrawAccount(String branchUuid) {
		return findUnique(new Queryable<PlatformWithdrawAccount>() {
			@Override
			public QueryWrapper<PlatformWithdrawAccount> toQueryWrapper() {
				return new QueryWrapper<PlatformWithdrawAccount>()
						.eq("branch_uuid", branchUuid)
						.eq("is_default", true);
			}
		});
	}
}
